<!--
 作者：ex_baiyc
 日期：2016-11-3
 功能：首页-知己彼-品牌对比-品牌详细评价对比
 参数：
 事件：
-->
<template>
  <div>
    <toolbar :view-title="viewTitle">
      <div class="leftButton"
           type="button"
           slot="left-button"
           v-link='{path: "/peer"}'>&nbsp;
      </div>
      <div class="rightButton"
           type="button"
           slot='right-button'>
        <div class="filterButton" @click="showFilter" v-show="!filterOpenState">
          <span class="filterIcon" :page-id="routeKey">&nbsp;</span>
          <span>筛选</span>
        </div>
        <div class="menuButton" @click="showSlideMenu" v-show="!filterOpenState">
          <span class="menuIcon">&nbsp;</span>
          <span>菜单</span>
        </div>
        <span v-show="filterOpenState" @click="doFilter" class="filterOver">完成</span>
      </div>
    </toolbar>
    <div class="content">
      <select-for-brand :select-a-text="'选择品牌A'"
                        :select-b-text="'选择品牌B'"
                        :choose-platforms = "choosePlatforms"
                        :default-brand="defaultBrand"
                        :default-org-id="defaultOrgId"
                        :default-category="defaultCategory"
                        :default-category-id="defaultCategoryId"></select-for-brand>
      <div class="filter-message-box clearfix">
        <a  v-if="defaultCategory !== ''" class="filter-condition" @click="showFilter('pinlei')" href="javascript:;">{{defaultCategory}}</a>
        <span>
          <a v-if="UrlParams.platform !== ''" class="filter-condition" @click="showFilter('pingtai')" href="javascript:;" v-text="UrlParams.platform"></a>
          <a v-if="UrlParams.platform === ''" class="filter-condition" @click="showFilter('pingtai')" href="javascript:;">所有平台</a>
        </span>
        <a class="filter-condition" @click="showFilter('shijian')" href="javascript:;">{{UrlParams.startTime | formatDate}} - {{UrlParams.endTime | formatDate}}</a>
      </div>
      <div class="split-line"></div>
      <condition-filter :condition-list="conditionList"></condition-filter>
      <div class="split-line"></div>
      <section class="section">
        <div class="sub-title">
          <div class="sub-title-text"><!--{{chartData1.subTitle}}-->{{defaultCategory}}品牌负面关键字对比</div>
         <!--  <div class="sub-title-time">{{UrlParams.startTime | formatDate}} - {{UrlParams.endTime | formatDate}}</div> -->
        </div>
        <bar-chart-view v-if="!noData" :chart-data='chartData' :chart-height="'51vh'" :chart-width="'90%'"></bar-chart-view>
        <div class="nodata-wrap" v-if="noData">
           <h1>暂无相关数据!</h1>
        </div>
      </section>
    </div>
    <filter-view v-show="filterOpenState"
                 :condition="conditions"
                 :choose-platforms="choosePlatforms"
                 :page-id="routeKey"
                 :default-start-date="UrlParams.startTime"
                 :default-end-date="UrlParams.endTime"
                 :default-org-id="defaultOrgId"
                 :default-category="defaultCategory"
                 :default-category-id="defaultCategoryId"></filter-view>
    <slide-menu  v-show="menuOpenState"></slide-menu>
    <page-turning :mkey="routeKey" :page-id="'peer'"></page-turning>
    <loading :show="showLoading"></loading>
   <!--  <time-out-page :is-time-out="isTimeOut"></time-out-page> -->
   <confirm :show.sync="timeoutStatus"
   title="加载失败,您可以"
   cancel-text="返回首页"
   confirm-text="重新加载"
   @on-cancel="this.$route.router.go('/home')"
   @on-confirm="reload"></confirm>
   <div class="loading-box">
      <img src="../../../../static/img/loading.gif" class="loading">
    </div>
  </div>
</template>

<script>
  import Toolbar from '../../common/Toolbar.vue'
  import SelectForBrand from '../../peer/selectForBrand.vue'
  import ConditionFilter from '../../common/ConditionFilter.vue'
  import BarChartView from '../../common/BarChartView.vue'
  import FilterView from '../../common/FilterView.vue'
  import SlideMenu from '../../peer/SlideMenu.vue'
  // import TimeOutPage from '../../common/TimeOutPage.vue'
  import pageTurning from '../../common/pageTurning.vue'
  import Loading from 'vux/dist/components/Loading'
  import Confirm from 'vux/dist/components/Confirm'

  export default{
    components: {
      Toolbar,
      SelectForBrand,
      ConditionFilter,
      BarChartView,
      FilterView,
      SlideMenu,
      pageTurning,
      Loading,
      Confirm
    },
    data () {
      return {
        choosePlatforms: '',
        noData: false,
        timeoutStatus: false,
        showLoading: false,
        filterOpenState: false,
        menuOpenState: false,
        viewTitle: '品牌评价详细对比',
        conditionList: [
          {'id': 0, 'value': 'purchase', 'text': '购买体验'},
          {'id': 1, 'value': 'product', 'text': '产品体验'},
          {'id': 2, 'value': 'service', 'text': '服务体验'}
        ],
        defaultBrand: '',
        defaultOrgId: '',
        defaultCategory: '',
        defaultCategoryId: '',
        chartData: '',
//        图表使用的 option的基本参数, 待请求完成后将请求结果加入
        chartData1: {
          subTitle: '',
          starttime: '',
          endtime: '',
          legend: [],
          yAxisData: [],
          splitNumber: 1,
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'bar',
              barGap: 0,
              barMaxWidth: 10,
              data: []
            },
            {
              name: '',
              type: 'bar',
              barGap: 0,
              barMaxWidth: 10,
              data: []
            }
          ]
        },
        chartData2: {
          subTitle: '',
          starttime: '',
          endtime: '',
          legend: [],
          yAxisData: [],
          splitNumber: 1,
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'bar',
              barGap: 0,
              barMaxWidth: 10,
              data: []
            },
            {
              name: '',
              type: 'bar',
              barGap: 0,
              barMaxWidth: 10,
              data: []
            }
          ]
        },
        chartData3: {
          subTitle: '',
          starttime: '',
          endtime: '',
          legend: [],
          yAxisData: [],
          splitNumber: 1,
          colors: ['#4A90E2', '#B889FF'],
          series: [
            {
              name: '',
              type: 'bar',
              barGap: 0,
              barMaxWidth: 10,
              data: []
            },
            {
              name: '',
              type: 'bar',
              barGap: 0,
              barMaxWidth: 10,
              data: []
            }
          ]
        },
//        请求参数
        UrlParams: {
          pageId: '67',
          mkey: 'brandDetailedEvaluation',
          dateSeg: 'other',
          startTime: '',
          endTime: '',
          category_id: '',
          category: '',
          brand1: '',
          brand2: '',
          brand: '',
          org_code: '',
          platform: ''
        }
      }
    },
    methods: {
//      超时后刷新
      reload: function () {
        window.location.reload()
      },
//      显示筛选窗
      showFilter: function () {
        this.filterOpenState = true
      },
//      执行筛选操作
      doFilter: function () {
        this.$broadcast('doFilter')
      },
//      显示页面导航菜单栏
      showSlideMenu: function () {
        this.menuOpenState = !this.menuOpenState
        var self = this
        setTimeout(function () {
          self.$broadcast('slideStart')
        }, 0)
      },
//      获取页面数据
      getData: function (params) {
        var _this = this
        this.$http.post(window.interface.peerData, params, {_timeout: 30000}).then(function (res) {
          if (res.json().error) {
            this.timeoutStatus = true
            return
          }
          document.getElementsByClassName('loading-box')[0].style.display = 'none'
          _this.chartData1.legend = []
          _this.chartData1.xAxisData = []
          _this.chartData1.yAxisData = []
          _this.chartData1.series[0].data = []
          _this.chartData1.series[1].data = []
          _this.chartData2.legend = []
          _this.chartData2.xAxisData = []
          _this.chartData2.yAxisData = []
          _this.chartData2.series[0].data = []
          _this.chartData2.series[1].data = []
          _this.chartData3.legend = []
          _this.chartData3.xAxisData = []
          _this.chartData3.yAxisData = []
          _this.chartData3.series[0].data = []
          _this.chartData3.series[1].data = []
          _this.showLoading = false
          var data = res.json()
          // if (!data.chartDatas[0] || data.chartDatas[0].detail.length === 0 || !data.chartDatas[1] || data.chartDatas[1].detail.length === 0 || !data.chartDatas[2] || data.chartDatas[2].detail.length === 0) {
          //   window.alert('当前条件下无数据,请重新选择!')
          //   this.filterOpenState = true
          //   return
          // }
          _this.modelA = data.brand1 || '选择品牌A'
          _this.modelB = data.brand2 || '选择品牌B'
          _this.defaultBrand = data.brand
          _this.UrlParams.startTime = data.startTime || ''
          _this.UrlParams.endTime = data.endTime || ''
          _this.defaultOrgId = data.org_code || ''
          _this.defaultCategory = _this.UrlParams.category = data.category || ''
          _this.defaultCategoryId = _this.UrlParams.category_id = data.category_id || ''
          this.choosePlatforms = data.platformCnd || data.platform
          this.$broadcast('loadDataDone')
          var chart1 = data.chartDatas[0].detail
          if (chart1.length === 0) {
            this.noData = true
          } else {
            this.noData = false
          }
          var chart2 = data.chartDatas[1].detail
          var chart3 = data.chartDatas[2].detail
          _this.chartData1.subTitle = data.chartDatas[0].subTitle
          _this.chartData1.starttime = data.chartDatas[0].starttime
          _this.chartData1.endtime = data.chartDatas[0].endtime
          _this.chartData1.legend.push({name: chart1[0].brand, icon: 'rect'})
          _this.chartData1.legend.push({name: chart1[1].brand, icon: 'rect'})
          _this.chartData2.legend.push({name: chart2[0].brand, icon: 'rect'})
          _this.chartData2.legend.push({name: chart2[1].brand, icon: 'rect'})
          _this.chartData3.legend.push({name: chart3[0].brand, icon: 'rect'})
          _this.chartData3.legend.push({name: chart3[1].brand, icon: 'rect'})
          _this.chartData1.series[0].name = chart1[0].brand
          _this.chartData1.series[1].name = chart1[1].brand
          _this.chartData2.series[0].name = chart2[0].brand
          _this.chartData2.series[1].name = chart2[1].brand
          _this.chartData3.series[0].name = chart3[0].brand
          _this.chartData3.series[1].name = chart3[1].brand

          for (var i = 0; i < chart1.length; i++) {
            if (i % 2 === 0) {
              _this.chartData1.yAxisData.push(chart1[i].keyword)
              _this.chartData1.series[0].data.push(chart1[i].value)
            } else if (i % 2 === 1) {
              _this.chartData1.series[1].data.push(chart1[i].value)
            }
          }
          for (var j = 0; j < chart2.length; j++) {
            if (j % 2 === 0) {
              _this.chartData2.yAxisData.push(chart2[j].keyword)
              _this.chartData2.series[0].data.push(chart2[j].value)
            } else if (j % 2 === 1) {
              _this.chartData2.series[1].data.push(chart2[j].value)
            }
          }
          for (var k = 0; k < chart3.length; k++) {
            if (k % 2 === 0) {
              _this.chartData3.yAxisData.push(chart3[k].keyword)
              _this.chartData3.series[0].data.push(chart3[k].value)
            } else if (k % 2 === 1) {
              _this.chartData3.series[1].data.push(chart3[k].value)
            }
          }
        }, (res) => {
          this.timeoutStatus = true
        })
      }
    },
    events: {
//      接收filter组件回传回来的值，然后再根据值重新请求页面数据
      'filterConditions': function (filterConditions) {
//        this.UrlParams.department = filterConditions.department || ''
        this.UrlParams.category = filterConditions.category || ''
        this.UrlParams.category_id = this.defaultCategoryId = filterConditions.category_id || ''
        this.UrlParams.platform = filterConditions.platform || ''
        this.UrlParams.platform_id = filterConditions.platform_id || ''
        this.UrlParams.startTime = filterConditions.starttime || ''
        this.UrlParams.endTime = filterConditions.endtime || ''
        this.UrlParams.dateSeg = filterConditions.dateSeg || ''
        this.UrlParams.org_code = filterConditions.org_code || ''
        this.UrlParams.brand = ''
        this.UrlParams.brand1 = ''
        this.UrlParams.brand2 = ''
        // this.showLoading = true
        document.getElementsByClassName('loading-box')[0].style.display = 'block'
        this.getData(this.UrlParams)
      },
//      切换体验指标
      'changeCondition': function (id) {
        switch (id) {
          case 0:
            this.chartData = this.chartData1
            break
          case 1:
            this.chartData = this.chartData2
            break
          case 2:
            this.chartData = this.chartData3
            break
        }
      },
//      选择品牌后根据选择的品牌重新获取数据
      'chooseBrand': function (brandA, brandB) {
        this.UrlParams.brand1 = brandA
        this.UrlParams.brand2 = brandB
        this.UrlParams.brand = brandA + ',' + brandB
        // this.showLoading = true
        document.getElementsByClassName('loading-box')[0].style.display = 'block'
        this.getData(this.UrlParams)
      }
    },
    ready: function () {
      // this.showLoading = true
      this.getData(this.UrlParams)
      this.chartData = this.chartData1
      this.routeKey = this.$route.query.key
    },
    created: function () {
      this.routeKey = this.$route.query.key
    }
  }
</script>

<style lang="scss" scoped>
  .content {
    position: relative;
    margin-top: 8.2vh;
    margin-bottom: 8vh;
  }

  .sub-title {
    margin-bottom: .2rem;
    padding: .2rem;
    font-size: .28rem;
    color: #000;
    border-bottom: 1px solid #f2f2f2;

  .sub-title-text {
    padding-left: .7rem;
    background: url('../../../../static/img/time-title2.png') no-repeat .3rem center / .28rem .26rem;
  }

  .sub-title-time {
    padding-left: .7rem;
    background: url('../../../../static/img/time-time.png') no-repeat .3rem center / .28rem .26rem;
    font-size: .26rem;
    margin-top: 1.4vh;
    height: 3vh;
    line-height: 3vh;
    color: #999;
  }

  }

  .split-line {
    width: 100%;
    height: 5px;
    background: #f2f2f2;
  }

  .time {
    padding-left: .7rem;
    margin-top: 1.4vh;
    margin-bottom: 1.4vh;
    height: 3vh;
    line-height: 3vh;
    font-size: .26rem;
    background: url('../../../../static/img/time-time.png') no-repeat .3rem center / .28rem .26rem;
    color: #999;
  }

  .filter-message-box{
    padding: 0rem .2rem .2rem;
    font-size: 0.28rem;
  }

  .nodata-wrap{
    padding-top: 2rem;
    padding-bottom: 2rem;
    background: #FFF;
    text-align: center;
  }
</style>
